﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace Poligono.Web
{
    /// <summary>
    /// Class that is used to perform operations related to Form components.
    /// </summary>
    public class FormUtil
    {
        #region DropDownList
        /// <summary>
        /// Method that inserts a default value as the first item of dropdownlist
        /// </summary>
        /// <param name="sender">DropDownList object passed as parameter</param>
        public static void ConfigDropDownList(object sender) 
        {
            ConfigDropDownList(sender, ".:Select an item:.", string.Empty);
        }

        /// <summary>
        /// Method that inserts a text passed as parameter in dropdownlist
        /// </summary>
        /// <param name="sender">variable object passed as parameter</param>
        /// <param name="display">what is shown on the 1st line of the dropdownlist</param>
        public static void ConfigDropDownList(object sender, string display)
        {
            ConfigDropDownList(sender, display, string.Empty);
        }

        /// <summary>
        /// Method that inserts a text passed as parameter in dropdownlist, and the value of this line.
        /// </summary>
        /// <param name="sender">variable object passed as parameter</param>
        /// <param name="display">what is shown on the 1st line of the dropdownlist</param>
        /// <param name="value">the value of the first line of the dropdownlist</param>
        public static void ConfigDropDownList(object sender, string display, string value)
        {
            DropDownList ddl = (DropDownList)sender;
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem(display, value));
        }
        #endregion DropDownList

        #region RadioButtonList
        /// <summary>
        /// Method that inserts the items passed as parameter in a radiobuttonlist, include their own values.
        /// </summary>
        /// <param name="sender">Object that representes the RadioButtonList</param>
        /// <param name="items">The items that will be inserted in the RadioButtonList</param>
        public static void ConfigRadioButtonList(object sender, params ListItem[] items) 
        {
            RadioButtonList rbl = (RadioButtonList)sender;
            rbl.Items.Clear();
            for (int i = 0; i < items.Length; i++) 
            {
                rbl.Items.Add(items[i]);
            }
        }
        #endregion RadioButtonList

        #region CheckBoxList
        /// <summary>
        /// Method that inserts the items passed as parameter in a CheckBoxList, include their own values.
        /// </summary>
        /// <param name="sender">Object that representes the CheckBoxList</param>
        /// <param name="items">The items that will be inserted in the CheckBoxList</param>
        public static void ConfigCheckBoxList(object sender, params ListItem[] items) 
        {
            CheckBoxList cbl = (CheckBoxList)sender;
            cbl.Items.Clear();
            for (int i = 0; i < items.Length; i++) 
            {
                cbl.Items.Add(items[i]);
            }
        }
        #endregion CheckBoxList

        #region Other Methods for several Web Components
        /// <summary>
        /// Transfer a ListBox item to another ListBox.
        /// </summary>
        /// <param name="listSource">Listbox that contains the item(s) to be transfered to another ListBox</param>
        /// <param name="listTo">ListBox that will receive the item(s) from another ListBox</param>
        public static void ListboxTransfer(ListBox listSource, ListBox listTo)
        {
            int[] indices = listSource.GetSelectedIndices();
            if (indices.Length == 0)
            {
                for (int i = 0; i < listSource.Items.Count; i++)
                {
                    listTo.Items.Add(listSource.Items[i]);
                    listSource.Items.RemoveAt(i);
                }
            }
            else 
            {
                foreach(int i in indices)
                {
                    listTo.Items.Add(listSource.Items[i]);
                    listSource.Items.RemoveAt(i);
                }
            }
        }

        /// <summary>
        /// Method that return the control inside another control. It's necessary to pass, as parameter, the control collection 
        /// and the control name. it's necessary also pass the type of the control.
        /// </summary>
        /// <typeparam name="T">Type of return and control</typeparam>
        /// <param name="controls"></param>
        /// <param name="stringId">the control name</param>
        /// <returns>Typed control</returns>
        public static T GetControl<T>(ControlCollection controls, string stringId) where T: Control
        {
            foreach (Control control in controls) 
            {
                if (control.Controls.Count > 0) 
                {
                    T obj = GetControl<T>(control.Controls, stringId);
                    if (obj != null) 
                    {
                        return obj;
                    }
                }
                if (control is T) 
                {
                    if (control.ID == stringId) 
                    {
                        return (T)control;
                    }
                }
            }
            return null;
        }

        #endregion Another Methods for several Web Components
    }
}